<?php

namespace app\api\controller;

use think\Controller;
use app\common\lib\exception\ApiException;
use app\common\lib\Aes;
use app\common\lib\IAuth;
use think\Cache;
/*****Api模块公共的控制器********/
class Common extends Controller
{
     /**
     * headers信息
     * @var string
     */
    public $headers = '';

    public $page = 1;
    public $size = 10;
    public $from = 0;
    /****初始化的方法**** */
    public function _initialize()
    {
        $this->checkRequestAuth();
        //$this->testAes();
    }
    /****检查每次app请求数据是否合法**** */
    public function checkRequestAuth()
    {
        $headers = Request()->header();
        if(empty($headers['sign'])){
            throw new ApiException('sign不存在', 400);
        }
        if(!in_array($headers['apptype'],config('app.apptypes'))){
            throw new ApiException('app_type不合法', 400);
        }
          // 需要sign
        if(!IAuth::checkSignPass($headers)) {
            throw new ApiException('授权码sign失败', 401);
        }
        Cache::set($headers['sign'], 1, config('app.app_sign_cache_time'));
        // 1、文件  2、mysql 3、redis
        $this->headers = $headers;
    
    }

    public function testAes()
    {
      
         $data = [
             'a234'=>1123,
             'b234'=>345345,
         ];
       //  echo  IAuth::setSign($data);exit;
       
         $str = "FZd77TcDIC6AzKa+eN4ZIeDuQSGxZmil0kBtIap/3rY=";
         $d =  (new Aes())->decrypt($str);
         var_dump($d);
         exit;
    }
}